//
//  JobListFilterView.m
//  MyProject
//
//  Created by 石小强 on 2018/5/3.
//  Copyright © 2018年 yanjiaming. All rights reserved.
//

#import "JobListFilterView.h"
#import "StringAndIndexModel.h"
#import "FilterButton.h"

@interface JobListFilterView()

@property (nonatomic, strong) UIWindow *window;
@property (nonatomic, strong) UIView *backGroundView;
@property (nonatomic, strong) UIView *topGroundView;
@property (nonatomic, strong) NSArray *dataArray;
@property (nonatomic, strong) UIScrollView *backScrollView;

@end

@implementation JobListFilterView {
    NSString *_educational;
    NSString *_experience;
    NSString *_minSalary;
    NSString *_maxSalary;
    NSString *_jobNature;
}


- (void)show {
    // 1.获取最上面的窗口
    UIWindow *window = [UIApplication sharedApplication].keyWindow;
    self.window = window;
    
    // 2.添加蒙版
    UIView *topGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, NAV_STATUS_H + 40)];
    topGroundView.backgroundColor = [UIColor clearColor];
    self.topGroundView = topGroundView;
    [window addSubview:topGroundView];
    UITapGestureRecognizer *tgr1= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismissExt)];
    [topGroundView addGestureRecognizer:tgr1];
    
    UIView *backGroundView = [[UIView alloc] initWithFrame:CGRectMake(0, NAV_STATUS_H + 40, kScreenWidth, kScreenHeight - NAV_STATUS_H - 40)];
    backGroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
    self.backGroundView = backGroundView;
    [window addSubview:backGroundView];
    UITapGestureRecognizer *tgr= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(dismiss)];
    [backGroundView addGestureRecognizer:tgr];
    
    // 3.添加自己到蒙版上
    [backGroundView addSubview:self];
    
    // 4.设置尺寸
    self.frame = window.bounds;
    
    // 5.初始化子控件
    [self setupControls];
    
//    backGroundView.backgroundColor = [UIColor clearColor];
//    self.alpha = 0;
//    [UIView animateWithDuration:0.25 animations:^{
//        backGroundView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.7];
//        self.alpha = 1;
//    }];
}

- (void)setupControls {
    // 530
    CGFloat height = kScreenHeight - NAV_STATUS_H - 40 > 530 ? 530 : kScreenHeight - NAV_STATUS_H - 40;
    self.backScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, height)];
    self.backScrollView.backgroundColor = [UIColor whiteColor];
    self.backScrollView.contentSize = CGSizeMake(0, 530);
    [self addSubview:self.backScrollView];
    
    UITapGestureRecognizer *tgr1= [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(test)];
    [self.backScrollView addGestureRecognizer:tgr1];
    
    // 学历要求
    UILabel *titleL1 = [[UILabel alloc] initWithFrame:CGRectMake(15, 24, 100, 11)];
    titleL1.text = @"学历要求";
    titleL1.textColor = UIColorFromRGB(0xA5B9C2);
    titleL1.font = [UIFont fontWithName:@"PingFangSC-Regular" size:11];
    [self.backScrollView addSubview:titleL1];
    NSArray *datas1 = @[@"不限", @"中专及以下", @"高中", @"大专", @"本科", @"硕士", @"博士"];
    CGFloat btnY1 = 50;
    CGFloat btnX1 = 15;
    for (NSInteger i = 0; i < datas1.count; i++) {
        NSString *btnTitle = [datas1 objectAtIndex:i];
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        FilterButton *btn = [[FilterButton alloc] initWithFrame:CGRectMake(btnX1, btnY1, btnTitleWidth, 27)];
        btn.tag = 50 + i;
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.backScrollView addSubview:btn];
        btnX1 += btnTitleWidth + 15;
        
        if (i == 0) {
            btn.selected = YES;
        } else {
            btn.selected = NO;
        }
        
        if (i == 3) {
            btnY1 = 91;
            btnX1 = 15;
        }
    }
    
    /** 工作经验 */
    UILabel *titleL2 = [[UILabel alloc] initWithFrame:CGRectMake(15, 142, 100, 11)];
    titleL2.text = @"工作经验";
    titleL2.textColor = titleL1.textColor;
    titleL2.font = titleL1.font;
    [self.backScrollView addSubview:titleL2];
    NSArray *datas2 = @[@"不限", @"应届生", @"1年以下", @"1-3年", @"3-5年", @"5-10年", @"10年以上"];
    CGFloat btnY2 = 168;
    CGFloat btnX2 = 15;
    for (NSInteger i = 0; i < datas2.count; i++) {
        NSString *btnTitle = [datas2 objectAtIndex:i];
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        FilterButton *btn = [[FilterButton alloc] initWithFrame:CGRectMake(btnX2, btnY2, btnTitleWidth, 27)];
        btn.tag = 100 + i;
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.backScrollView addSubview:btn];
        btnX2 += btnTitleWidth + 15;
        
        if (i == 0) {
            btn.selected = YES;
        } else {
            btn.selected = NO;
        }
        
        if (i == 3) {
            btnY2 = 210;
            btnX2 = 15;
        }
    }
    
    /** 薪水(单选) */
    UILabel *titleL3 = [[UILabel alloc] initWithFrame:CGRectMake(15, 260, 260, 11)];
    titleL3.text = @"薪水(单选)";
    titleL3.textColor = titleL1.textColor;
    titleL3.font = titleL1.font;
    [self.backScrollView addSubview:titleL3];
    NSArray *datas3 = @[@"不限", @"3k以下", @"3k-5k", @"5k-10k", @"10k-20k", @"50k以上"];
    CGFloat btnY3 = 286;
    CGFloat btnX3 = 15;
    for (NSInteger i = 0; i < datas3.count; i++) {
        NSString *btnTitle = [datas3 objectAtIndex:i];
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        FilterButton *btn = [[FilterButton alloc] initWithFrame:CGRectMake(btnX3, btnY3, btnTitleWidth, 27)];
        btn.tag = 150 + i;
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.backScrollView addSubview:btn];
        btnX3 += btnTitleWidth + 15;
        
        if (i == 0) {
            btn.selected = YES;
        } else {
            btn.selected = NO;
        }
        
        if (i == 3) {
            btnY3 = 327;
            btnX3 = 15;
        }
    }
    
    /** 工作性质 */
    UILabel *titleL4 = [[UILabel alloc] initWithFrame:CGRectMake(15, 379, 379, 11)];
    titleL4.text = @"工作性质";
    titleL4.textColor = titleL1.textColor;
    titleL4.font = titleL1.font;
    [self.backScrollView addSubview:titleL4];
    NSArray *datas4 = @[@"不限", @"兼职", @"实习", @"全职"];
    CGFloat btnY4 = 404;
    CGFloat btnX4 = 15;
    for (NSInteger i = 0; i < datas4.count; i++) {
        NSString *btnTitle = [datas4 objectAtIndex:i];
        CGFloat btnTitleWidth = [Tool sizeWithText:btnTitle font:[UIFont fontWithName:@"PingFangSC-Regular" size:13]].width + 30;
        FilterButton *btn = [[FilterButton alloc] initWithFrame:CGRectMake(btnX4, btnY4, btnTitleWidth, 27)];
        btn.tag = 200 + i;
        [btn setTitle:btnTitle forState:UIControlStateNormal];
        [btn addTarget:self action:@selector(btn1Click:) forControlEvents:UIControlEventTouchUpInside];
        [self.backScrollView addSubview:btn];
        btnX4 += btnTitleWidth + 15;
        
        if (i == 0) {
            btn.selected = YES;
        } else {
            btn.selected = NO;
        }
    }
    
    // 确认按钮
    UIButton *okBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, 481, kScreenWidth, 49)];
    [okBtn setBackgroundColor:UIColorFromRGB(0x00C6D5)];
    [okBtn setTitle:@"确认" forState:UIControlStateNormal];
    [okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    okBtn.titleLabel.font = [UIFont fontWithName:@"PingFangSC-Regular" size:17];
    [okBtn addTarget:self action:@selector(okBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.backScrollView addSubview:okBtn];
}

- (void)btn1Click:(FilterButton *)btn {
    if (btn.selected) {
        return;
    } else {
        btn.selected = YES;
        if (btn.tag >= 50 && btn.tag < 60) {
            _educational = [self getEducationWithTag:btn.tag - 50];
            for (UIControl *cont in self.backScrollView.subviews) {
                if ([cont isKindOfClass:[FilterButton class]]) {
                    if (cont.tag >= 50 && cont.tag < 60 && cont.tag != btn.tag) {    // 学历要求
                        FilterButton *everyB = (FilterButton *)cont;
                        everyB.selected = NO;
                    }
                }
            }
        } else if (btn.tag >= 100 && btn.tag < 110) { // 工作经验
            _experience = [self getExperienceWithTag:btn.tag - 100];
            for (UIControl *cont in self.backScrollView.subviews) {
                if ([cont isKindOfClass:[FilterButton class]]) {
                    if (cont.tag >= 100 && cont.tag < 110 && cont.tag != btn.tag) {    // 工作经验
                        FilterButton *everyB = (FilterButton *)cont;
                        everyB.selected = NO;
                    }
                }
            }
        } else if (btn.tag >= 150 && btn.tag < 160) { // 薪水
            btn.selected = YES;
            if (btn.tag - 150 == 0) {
                _minSalary = @"";
                _maxSalary = @"";
            } else if (btn.tag - 150 == 1) {
                _minSalary = @"";
                _maxSalary = @"3";
            } else if (btn.tag - 150 == 2) {
                _minSalary = @"3";
                _maxSalary = @"5";
            } else if (btn.tag - 150 == 3) {
                _minSalary = @"5";
                _maxSalary = @"10";
            } else if (btn.tag - 150 == 4) {
                _minSalary = @"10";
                _maxSalary = @"20";
            } else if (btn.tag - 150 == 5) {
                _minSalary = @"20";
                _maxSalary = @"50";
            } else {
                _minSalary = @"";
                _maxSalary = @"";
            }
            for (UIControl *cont in self.backScrollView.subviews) {
                if ([cont isKindOfClass:[FilterButton class]]) {
                    if (cont.tag >= 150 && cont.tag < 160 && cont.tag != btn.tag) {    // 工作经验
                        FilterButton *everyB = (FilterButton *)cont;
                        everyB.selected = NO;
                    }
                }
            }
        } else if (btn.tag >= 200 && btn.tag < 210) {  // 工作性质
            _jobNature = [self getJobNatureWithTag:btn.tag - 200];
            for (UIControl *cont in self.backScrollView.subviews) {
                if ([cont isKindOfClass:[FilterButton class]]) {
                    if (cont.tag >= 200 && cont.tag < 210 && cont.tag != btn.tag) {    // 工作性质
                        FilterButton *everyB = (FilterButton *)cont;
                        everyB.selected = NO;
                    }
                }
            }
        }
        
    }
}

- (NSString *)getEducationWithTag:(NSInteger)tag {
    if (tag == 0) {
        return @"";
    } else if (tag == 1) {
        return @"EDUCATION_ZH";
    } else if (tag == 2) {
        return @"EDUCATION_GZ";
    } else if (tag == 3) {
        return @"EDUCATION_ZK";
    }  else if (tag == 4) {
        return @"EDUCATION_XS";
    }  else if (tag == 5) {
        return @"EDUCATION_SX";
    }  else if (tag == 6) {
        return @"EDUCATION_BS";
    }  else {
        return @"";
    }
}

- (NSString *)getExperienceWithTag:(NSInteger)tag {
    if (tag == 0) {
        return @"";
    } else if (tag == 1) {
        return @"JOB_WORK_YEAR_GT_0LT_0";
    } else if (tag == 2) {
        return @"JOB_WORK_YEAR_LT_0";
    } else if (tag == 3) {
        return @"JOB_WORK_YEAR_GT_1_LT_0";
    }  else if (tag == 4) {
        return @"JOB_WORK_YEAR_GT_5_LT_3";
    }  else if (tag == 5) {
        return @"JOB_WORK_YEAR_GT_10_LT_5";
    }  else if (tag == 6) {
        return @"JOB_WORK_YEAR_GT_10";
    }  else {
        return @"";
    }
}

- (NSString *)getJobNatureWithTag:(NSInteger)tag {
    if (tag == 0) {
        return @"";
    } else if (tag == 1) {
        return @"POSITION_NATURE_PART_TIME";
    } else if (tag == 2) {
        return @"POSITION_NATURE_PRACTICE";
    } else {
        return @"POSITION_NATURE_FULL_TIME";
    }
}

- (void)okBtnClick {
    [self dismiss];
}

- (void)dismiss {
    [self.backScrollView removeFromSuperview];
    [self.backGroundView removeFromSuperview];
    [self.topGroundView removeFromSuperview];
    self.dismissBlock(_educational, _experience, _minSalary, _maxSalary, _jobNature);
    [self removeFromSuperview];
}

- (void)dismissExt {
    [self.backScrollView removeFromSuperview];
    [self.backGroundView removeFromSuperview];
    [self.topGroundView removeFromSuperview];
    self.justDismissBlock();
    [self removeFromSuperview];
}

- (void)test {
    NSLog(@"点击了scrollview");
}

@end
